package soar.template.springboot.service.hello.configuration.job;

import javax.annotation.Resource;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.quartz.CronTriggerFactoryBean;
import org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;

import soar.template.springboot.service.hello.job.QuartzJob;

@Configuration
class MyQuartzConfiguration
{
    @Resource
    private QuartzJob quartzJob;

    @Bean
    public MethodInvokingJobDetailFactoryBean methodQuartzJob()
    {
        MethodInvokingJobDetailFactoryBean method = new MethodInvokingJobDetailFactoryBean();
        method.setTargetObject(quartzJob);
        method.setTargetMethod("execute");
        method.setConcurrent(false);
        return method;
    }

    @Bean
    public CronTriggerFactoryBean cronTriggerRefreshBufferJob()
    {
        CronTriggerFactoryBean trigger = new CronTriggerFactoryBean();
        trigger.setJobDetail(methodQuartzJob().getObject());
        trigger.setCronExpression("0/5 * * * * ?");
        return trigger;
    }

    @Bean
    public SchedulerFactoryBean schedulerFactory()
    {
        SchedulerFactoryBean scheduler = new SchedulerFactoryBean();
        scheduler.setTriggers(cronTriggerRefreshBufferJob().getObject());
        return scheduler;
    }
}
